import { createHaru, getHarusByTags } from '../utils/haru'

class Comment {
    author: number
    reason: string

    constructor(data: string) {
        const { author, reason } = JSON.parse(data)
        this.author = author
        this.reason = reason
    }
}

class User {
    private warns: Comment[] | undefined = undefined
    private praises: Comment[] | undefined = undefined
    uid: number
    constructor(uid: number) {
        this.uid = uid
    }

    async getWarns(): Promise<Comment[]> {
        if (this.warns === undefined) {
            const warns = (await getHarusByTags(['user warn', `${this.uid}`]))
            this.warns = warns.map(haru => new Comment(haru.content))
        }
        return this.warns
    }

    async addWarn(warn: Comment): Promise<number> {
        this.warns = undefined
        return await createHaru(JSON.stringify(warn), ['user warn', `${this.uid}`])
    }

    async getPraises(): Promise<Comment[]> {
        if (this.praises === undefined) {
            const praises = (await getHarusByTags(['user praise', `${this.uid}`]))
            this.praises = praises.map(haru => new Comment(haru.content))
        }
        return this.praises
    }

    async addPraise(praise: Comment): Promise<number> {
        this.praises = undefined
        return await createHaru(JSON.stringify(praise), ['user praise', `${this.uid}`])
    }
}

export default User
